// ajax({
//     url:"",
//     data:{},
//     type:"",
//     success:()=>{},
//     error:()=>{}
// })

function ajax(ops = {}){
    let {url,data={},type="get",success,error} = ops;

    
    let str = "";
    for(let i in data){
        str += `${i}=${data[i]}&`;
    }
    str = str.slice(0,str.length-1);
    
    url = type==="get" ? url+"?"+str : url;
    
    const xhr = new XMLHttpRequest();
    xhr.open(type, url, true);
    xhr.addEventListener("load",function(){
        if(xhr.status === 200){
            success(xhr.responseText);
        }else{
            error && error(xhr.status);
        }
    })
    if(type === "get"){
        xhr.send();
    }else{
        xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xhr.send(str);
    }
}